package com.hsh.maoduo.service.impl;

import com.hsh.maoduo.common.Result;
import com.hsh.maoduo.comparator.CountComparator;
import com.hsh.maoduo.constant.ExportPathConstants;
import com.hsh.maoduo.constant.common.GlobalPathConstants;
import com.hsh.maoduo.mapper.CountMapper;
import com.hsh.maoduo.pojo.dto.CountDTO;
import com.hsh.maoduo.pojo.export.CountExport;
import com.hsh.maoduo.pojo.vo.CountVO;
import com.hsh.maoduo.service.CountService;
import com.hsh.maoduo.util.DataUtil;
import com.hsh.maoduo.util.MyBeanUtils;
import com.hsh.maoduo.util.POIExcelUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CountServiceImpl implements CountService {

    @Resource
    private CountMapper countMapper;

    @Override
    public List<CountDTO> getList(CountVO countVO) {
        countVO.init();
        List<CountDTO> countDTOList = new ArrayList<>();
        switch (countVO.getInOutType()) {
            case 0:
                countDTOList = countMapper.selectCountIn(countVO);
                countDTOList.addAll(countMapper.selectCountOut(countVO));
                countDTOList.sort(CountComparator::compare);
                break;
            case 1:
                countDTOList = countMapper.selectCountIn(countVO);
                break;
            case 2:
                countDTOList = countMapper.selectCountOut(countVO);
                break;
        }
        return countDTOList;
    }

    @Override
    public Result countExport(CountVO countVO) {
        List<CountDTO> countDTOList = getList(countVO);
        List<CountExport> countExportList = MyBeanUtils.copyList(countDTOList, CountExport.class);
        countExportList.stream().forEach(CountExport::init);
        String pathPre = "";
        String dateRange = DataUtil.removeSpaceAndSymbols(countVO.getStartDateTime())
                + "-"
                + DataUtil.removeSpaceAndSymbols(countVO.getEndDateTime()) + "-";
        switch (countVO.getDateType()) {
            case "DATE":
                pathPre = ExportPathConstants.DATE_COUNT_PATH_PRE + ExportPathConstants.DATE_COUNT_NAME_PRE;
                break;
            case "WEEK":
                pathPre = ExportPathConstants.WEEK_COUNT_PATH_PRE + ExportPathConstants.WEEK_COUNT_NAME_PRE;
                break;
            case "MONTH":
                pathPre = ExportPathConstants.MONTH_COUNT_PATH_PRE + ExportPathConstants.MONTH_COUNT_NAME_PRE;
                break;
        }
        int rowNum = POIExcelUtil.write(pathPre + dateRange + System.currentTimeMillis() + GlobalPathConstants.XLSX_SUF
                , countExportList, CountExport.class);
        return Result.create(rowNum);
    }
}
